<?php

function editVideos($aItem)
{
	isUser();

	$oRespX = new xajaxResponse();

	if ( empty($aItem['val']['vid_title']) )
	{
		$oRespX->addScript("alert('". App::format('Mod_Video.please_add_a_title') ."');");
		return $oRespX;
	}

	$oSrvVideo = &App::getModuleService('Video', 'Video');

	if ( isset($aItem['val']['vid_url']) )
	{
		$aError = array();
		if ( $aItem['val']['stream_id'] == '0' && !$oSrvVideo->isAllowedExt($aItem['val']['vid_url']) )
		{
			$aError[] = App::format('Mod_Video.upload_file_invalid', App::getParam('vid_allowed_ext'));
		}

		if ( $aItem['val']['stream_id'] != '0' )
		{
			foreach(App::getParam('Mod_Video.stream_sources') as $aStream)
			{
				if ( $aStream['id'] == $aItem['val']['stream_id'] )
				{
					if ( !eregi($aStream['url'], $aItem['val']['vid_url']) )
					{
						$aError[] = App::format('Mod_Video.invalid.url', stripslashes($aStream['url']));
					}
					break;
				}
			}
		}

		if ( count($aError) )
		{

			foreach( $aError as $sError )
			{
				$oRespX->addScript("alert('". $sError ."');");
			}
			return $oRespX;
		}
	}

	$oSrvVideo->editVideo($aItem['val']);
	$oRespX->addScript("new Effect.Appear('edit_". $aItem['val']['vid_id'] ."');");

	return $oRespX;
}

function videoInviteUser($sUser, $iId, $sValue)
{
	isUser();

	$oRespX = new xajaxResponse();

	$oMessengerService = &App::getModuleService('Messenger', 'Messenger');
	$sUrl = App::makeUrl('public.videos', array('id' => $iId));
	$sInviteText = App::format('Mod_Video.mail_user_invited', array($sUrl, $sUrl));
	$oMessengerService->sendMessage(array('to'=>$sUser, 'title'=>App::format('Mod_Video.title_video_invite'),'text'=>$sInviteText), App::getUser('user'), 'mail');

	$info = " [ ". App::format($sValue) ." ] ";
	$oRespX->addAssign("user_". $sUser ."","innerHTML",$info);

	return $oRespX;
}

function latestVideos($iNum, $sAction = "")
{
	$oRespX = new xajaxResponse();

	if ( $sAction == 'minus' )
	{
		$iNum = ($iNum-1);
	}
	if ( $sAction == 'add' )
	{
		$iNum = ($iNum+1);
	}

	$sParams = array
	(
		'iNum' => $iNum,
	);

	$oSrvVideo = &App::getModuleComponent('Video', 'AjaxLatest', $sParams);

	list($sContent, $iCnt) = $oSrvVideo->process();

	$oRespX->addAssign("latest_added","innerHTML", $sContent);
	$oRespX->addScript("document.getElementById('current_page_count').value='". ($iNum) ."';");
	$oRespX->addScript("document.getElementById('ajax_image_latest').style.display='none';");

	return $oRespX;
}

function videoBox($sType)
{
	$oRespX = new xajaxResponse();

	$sParams = array
	(
		'sType' => $sType,
	);

	$oRespX->addAssign("info_title","innerHTML", App::format('Mod_Video.ajaxbox.' . $sType));
	$oSrvVideo = &App::getModuleComponent('Video', 'AjaxBox', $sParams);
	$oRespX->addAssign("video_box","innerHTML", $oSrvVideo->process());
	$oRespX->addScript("document.getElementById('ajax_image_box').style.display='none';");

	return $oRespX;
}

function videoAddFeature($iId, $sDivId)
{
	if ( !App::isStaff() )
	{
		exit();
		return;
	}

	$oRespX = new xajaxResponse();

	$oSrvVideo = &App::getModuleService('Video', 'Video');
	$oSrvVideo->addFeature($iId);
	$oRespX->addAssign($sDivId, "innerHTML", '<a href="javascript:void(0);" onclick="xajax_videoRemoveFeature(\'' . $iId . '\', \'feature_content_' . $iId . '\');">' . App::format('Mod_Video.remove_feature') . '</a>');
	$oRespX->addScript("alert('". App::format('Mod_Video.feature.done') ."');");

	return $oRespX;
}

function videoHotAdd($iId, $sDivId)
{
	if ( !App::isStaff() )
	{
		exit();
		return;
	}

	$oRespX = new xajaxResponse();

	$oSrvVideo = &App::getModuleService('Video', 'Video');
	$oSrvVideo->addHot($iId);
	$oRespX->addAssign($sDivId, "innerHTML", "");
	$oRespX->addScript("alert('". App::format('Mod_Video.hot_vid_added') ."');");

	return $oRespX;
}

function videoRemoveFeature($iId, $sDivId)
{
	if ( !App::isStaff() )
	{
		exit();
		return;
	}

	$oRespX = new xajaxResponse();

	$oSrvVideo = &App::getModuleService('Video', 'Video');
	$oSrvVideo->removeFeature($iId);
	$oRespX->addAssign($sDivId, "innerHTML", '<a href="javascript:void(0);" onclick="xajax_videoAddFeature(\'' . $iId . '\', \'feature_content_' . $iId . '\');">' . App::format('Mod_Video.feature') . '</a>');
	$oRespX->addScript("alert('". App::format('Mod_Video.feature.remove.done') ."');");

	return $oRespX;
}

function setStars($iRating, $iId)
{
	$iRating=$iRating/2;
	$oRespX = new xajaxResponse();

	$oRespX->addScript("document.getElementById('ajax_rating_loader').style.display='none';");

	if ( ! App::getUser('id') )
	{
		$oRespX->addScript("document.getElementById('show_stars').style.display='';");
		$oRespX = loginBox($oRespX);
		return $oRespX;
	}
	
	$oDb = Database::get();
	$aRow = $oDb->getRow('
		SELECT vid_userid
		FROM ' . App::getT('videos') . '
		WHERE vid_id = "' . $oDb->escape($iId) . '"
	');
	
	if (isset($aRow['vid_userid']) && ($aRow['vid_userid'] == App::getUser('id')))
	{
		$oRespX->addScript("document.getElementById('show_stars').style.display='';");
		$oRespX->addScript("alert('" . App::format('Mod_Video.cannot_rate_own_video') . "');");
		return $oRespX;		
	}

	$oSrvVideo = &App::getModuleService('Video', 'Video');
	if ( !$oSrvVideo->addRating($iId, $iRating) )
	{
		$oRespX->addScript("document.getElementById('show_stars').style.display='';");
		$oRespX->addScript("alert('". App::format('Mod_Video.already_rated') ."');");
		return $oRespX;
	}

	$oRespX->addAssign("thanksMessage","innerHTML", "". App::format('Mod_Video.thanks_for_rating') ."");

	return $oRespX;
}

function videoReport($aPost)
{
	$oRespX = new xajaxResponse();

	if ( !App::getUser('id') )
	{
		$oRespX->addScript("alert('". App::format('Mod_Video.report_login') ."');");
		return $oRespX;
	}

	$oSrvVideo = &App::getModuleService('Video', 'Video');
	$oSrvVideo->reportVideo($aPost['report_id'], $aPost['report_call']);

	$oRespX->addScript("alert('". App::format('Mod_Video.video_reported') ."');");
	$oRespX->addScript("new Effect.SlideUp('report_video');");

	return $oRespX;
}

function videoSaveFavorite($iId)
{
	$oRespX = new xajaxResponse();

	if ( !App::getUser('id') )
	{
		$oRespX = loginBox($oRespX);
		return $oRespX;
	}

	$oSrvVideo = &App::getModuleService('Video', 'Video');
	if ( !$oSrvVideo->saveToFavorite($iId) )
	{
		$oRespX->addScript("alert('". App::format('Mod_Video.fav_already_added') ."');");
	}
	else
	{
		$oRespX->addScript("alert('". App::format('Mod_Video.fav_added') ."');");
	}

	return $oRespX;
}

?>